package com.wave.wavesystem.business.controller;

import com.mybatisflex.core.paginate.Page;
import com.wave.wavesystem.business.pojo.dto.bug.BugReportQueryRequest;
import com.wave.wavesystem.business.pojo.emums.bug.BugEmergencyEnum;
import com.wave.wavesystem.business.pojo.emums.bug.BugPriorityEnum;
import com.wave.wavesystem.business.pojo.emums.bug.BugResolveStatusEnum;
import wave.common.pojo.entity.KV;
import wave.common.pojo.enums.RStatusEnum;
 import wave.common.pojo.entity.Result;
import com.wave.wavesystem.common.util.Condition;
import com.wave.wavesystem.common.util.EnumUtil;
import com.wave.wavesystem.common.util.Func;
import com.wave.wavesystem.business.pojo.entity.BugReportEntity;
import com.wave.wavesystem.business.service.BugReportService;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import java.util.Arrays;
import java.util.List;

/**
 * @author 夕下风波
 * @date 2025-02-11
 * @description bug上报表 访问层
 */
@RestController
@RequestMapping("/bugReport")
@Tag(name = "bug上报表")
public class BugReportController {

    @Resource
    private BugReportService bugReportService;

    /**
     * 新增
     *
     * @param bugReport 新增实体
     */
    @Operation(summary = "保存")
    @PostMapping("/save")
    public Result<BugReportEntity> save(@RequestBody BugReportEntity bugReport) {
        bugReportService.save(bugReport);
        return Result.data(bugReport);
    }

    /**
     * 删除/批量删除
     *
     * @param ids id集合
     */
    @Operation(summary = "删除")
    @PostMapping("/remove")
    public Result<Boolean> remove(String ids) {
        List<String> idList = Func.strToStringList(ids);
        return Result.status(bugReportService.removeByIds(idList), RStatusEnum.SUCCESS);
    }

    /**
     * 更新
     *
     * @param bugReport 更新内容
     */
    @Operation(summary = "更新")
    @PostMapping("/update")
    public Result<BugReportEntity> update(@RequestBody BugReportEntity bugReport) {
        bugReportService.updateById(bugReport);
        return Result.data(bugReportService.getById(bugReport.getId()), RStatusEnum.SUCCESS);
    }

    /**
     * 详情查询
     *
     * @param bugReport 查询条件
     */
    @Operation(summary = "详情")
    @GetMapping("/detail")
    public Result<BugReportEntity> detail(BugReportQueryRequest bugReport) {
        return Result.data(bugReportService.getById(bugReport.getId()));
    }

    /**
     * 列表查询
     *
     * @param bugReportQueryRequest 查询条件
     */
    @Operation(summary = "列表")
    @GetMapping("/list")
    public Result<List<BugReportEntity>> list(BugReportQueryRequest bugReportQueryRequest) {
        BugReportEntity bugReport = new BugReportEntity();
        Func.beanCopy(bugReportQueryRequest, bugReport);
        return Result.data(bugReportService.list(Condition.getQueryWrapper(bugReport)));
    }

    /**
     * 条件分页查询
     *
     * @param bugReportQueryRequest 查询条件
     */
    @Operation(summary = "分页")
    @GetMapping("/page")
    public Result<Page<BugReportEntity>> page(BugReportQueryRequest bugReportQueryRequest) {
        BugReportEntity bugReport = new BugReportEntity();
        Func.beanCopy(bugReportQueryRequest, bugReport);
        return Result.data(bugReportService.page(new Page<>(bugReportQueryRequest.getPageNum(), bugReportQueryRequest.getPageSize()),
                Condition.getQueryWrapper(bugReport)));
    }

    /**
     * 获取bug紧急程度类型
     */
    @GetMapping("bug_emergency_types")
    public Result<List<KV<String, Integer>>> getBugEmergencyTypes() {
        List<KV<String, Integer>> emergencyTypes = EnumUtil.getKVAll(Arrays.asList(BugEmergencyEnum.values()),
                BugEmergencyEnum::getText, BugEmergencyEnum::getValue);
        return Result.data(emergencyTypes);
    }

    /**
     * 获取 优先级类型
     */
    @GetMapping("bug_priority_types")
    public Result<List<KV<String, String>>> getBugPriorityTypes() {
        List<KV<String, String>> priorityTypes = EnumUtil.getKVAll(Arrays.asList(BugPriorityEnum.values()),
                BugPriorityEnum::getText, BugPriorityEnum::getValue);
        return Result.data(priorityTypes);
    }

    /**
     * 获取 解决状态枚举
     */
    @GetMapping("bug_resolve_status")
    public Result<List<KV<String, Integer>>> getBugResolveStatus() {
        List<KV<String, Integer>> priorityTypes = EnumUtil.getKVAll(Arrays.asList(BugResolveStatusEnum.values()),
                BugResolveStatusEnum::getText, BugResolveStatusEnum::getValue);
        return Result.data(priorityTypes);
    }

}